﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FileProcess
{
    public partial class Form1 : Form
    {
        private string filePath;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "C# Files (*.cs)|*.cs|All Files(*.*)|*.*";
                if(openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filePath = openFileDialog.FileName;
                }
                label1.Text = "文件名：" + Path.GetFileName(filePath);

                string[] lines = File.ReadAllLines(filePath);
                int linesAmount = lines.Length;
                int wordsAmount = lines.Sum(line => Regex.Matches(line, @"\b\w+\b").Count);
                label2.Text = "原始行数：" + linesAmount.ToString();
                label3.Text = "原始单词数：" + wordsAmount.ToString();
                label4.Text = "格式化后行数：";
                label5.Text = "格式化后单词数：";
                listBox1.Items.Clear();
            }
            catch (Exception)
            {
                MessageBox.Show("打开文件失败！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                MessageBox.Show("请先选择文件！");
                return;
            }

            try
            {
                string[] lines = File.ReadAllLines(filePath);
                var formattedLines = lines.Where(line => !string.IsNullOrWhiteSpace(line) && !line.TrimStart().StartsWith("//")).ToArray();
                int linesAmount = formattedLines.Length;
                int wordsAmount = formattedLines.Sum(line => Regex.Matches(line, @"\b\w+\b").Count);
                label4.Text = "格式化后行数：" + linesAmount.ToString();
                label5.Text = "格式化后单词数：" + wordsAmount.ToString();

                listBox1.Items.Clear();
                var wf = CountWordFrequency(formattedLines);
                foreach (var entry in wf.OrderBy(w => w.Key))
                {
                    listBox1.Items.Add($"{entry.Key}: {entry.Value}" + Environment.NewLine);
                }
 
            }
            catch (Exception)
            {
                MessageBox.Show("格式化文件失败！");
            }
        }

        private Dictionary<string, int> CountWordFrequency(string[] lines)
        {
            var wordCounts = new Dictionary<string, int>();

            foreach (var line in lines)
            {
                var words = Regex.Matches(line, @"\b\w+\b");
                foreach (Match word in words)
                {
                    if (wordCounts.ContainsKey(word.Value))
                    {
                        wordCounts[word.Value]++;
                    }
                    else
                    {
                        wordCounts[word.Value] = 1;
                    }
                }
            }
            return wordCounts;
        }
    }
}
